<?php
define('IN_CORE', true);
require_once './core/core.php';
//定义当前请求的系统常量
define('G_CHARSET', 'utf-8');
define('NOW_TIME',      $_SERVER['REQUEST_TIME']);
define('REQUEST_METHOD',$_SERVER['REQUEST_METHOD']);
define('IS_GET',        REQUEST_METHOD =='GET' ? true : false);
define('IS_POST',       REQUEST_METHOD =='POST' ? true : false);
define('IS_PUT',        REQUEST_METHOD =='PUT' ? true : false);
define('IS_DELETE',     REQUEST_METHOD =='DELETE' ? true : false);
define('IS_AJAX',       ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') || !empty($_POST[C('VAR_AJAX_SUBMIT')]) || !empty($_GET[C('VAR_AJAX_SUBMIT')])) ? true : false);
$data = (array)simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA'],'SimpleXMLElement',LIBXML_NOCDATA);
if (empty($data)){
	exit;
}
$where = array('status' => '未付款','code' => $data['out_trade_no']);
$addmoney_record = M('member_addmoney_record');
$orderinfo = $addmoney_record->where($where)->find();
$key = include_once './core/config/key.inc.php';
if ($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS' && $data['trade_type'] == 'APP' && $key['wxpay']['app_id'] == $data['appid'] && $key['wxpay']['pay_key'] == $data['attach']){
	if(!empty($orderinfo) && $addmoney_record->where($where)->save(array('status' => '已付款','scookies' => 0))){
		if(M('member')->where(array('uid' => $orderinfo['uid']))->setInc('money',$orderinfo['money'])){
			ok();
			exit;
		}
	}
}elseif ($orderinfo['status'] == '已付款'){
	ok();
}

function ok(){
	$ok = <<<xml
<xml>
  <return_code><![CDATA[SUCCESS]]></return_code>
  <return_msg><![CDATA[OK]]></return_msg>
</xml>
xml;
	echo $ok;
}